/**
 * 
 */
package com.gmail.avereshchaga.web.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.gmail.avereshchaga.db.entity.IDivision;

/**
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 * 
 */

public class ServiceFunction {

    public static final Comparator<IDivision> SENIORITY_ORDER_DIVISION = new Comparator<IDivision>() {
        public int compare(IDivision d1, IDivision d2) {
            return d1.getName().compareTo(d2.getName());
        }
    };

    private String param;

    public void manipulationRequest(HttpServletRequest request,
            HttpSession session) {

        if (request.getParameter("searchQuery") != null) {
            session.setAttribute("param", request.getParameter("searchQuery"));
        }
        
        String cleaning;
        if (request.getParameter("cleaning") == null) {
            cleaning = "";
        } else {
            cleaning = request.getParameter("cleaning");
        }
        if (cleaning.equals("true")) {
            param = null;
            session.setAttribute("param", param);
        }

        param = (String) session.getAttribute("param");

    }

    public String transformationStringForName(String name) {
        name = name.trim();
        String firstLetter = name.substring(0, 1).toUpperCase();
        String lastString = name.substring(1).toLowerCase();
        name = firstLetter + lastString;
        return name;
    }

    public Date transformationStringInDateFormat(String date)
            throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
        Date birthdate = dateFormat.parse(date);
        return birthdate;
    }

    public String transformationDateFormatToString(Date birthdate) {
        DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
        String birthdateStr = dateFormat.format(birthdate);
        return birthdateStr;
    }

    /**
     * @return the param
     */
    public String getParam() {
        return param;
    }

    /**
     * @param param
     *            the param to set
     */
    public void setParam(String param) {
        this.param = param;
    }
}
